<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CanvasExtends</title>
    <link
      rel="shortcut icon"
      type="image/x-icon"
      href="/examples/public/favicon.ico"
    />
    <link rel="stylesheet" href="../css/common.css" />
  </head>

  <style>
    #app {
      width: 100%;
      height: 100%;
      position: relative;
      background-color: black;
    }
  </style>

  <body>
    <div id="app"></div>

    <script type="module">
      import * as THREE from "three";
      import * as Vis from "../../dist/Vis.es.js";
      import { v4 as getUUid } from "uuid";

      const canvasGenerator = new Vis.CanvasGenerator({
        width: 800,
        height: 160,
      })
        .draw((ctx) => {
          ctx.textBaseline = "middle";
          ctx.textAlign = "center";

          ctx.fillStyle = "rgb(206, 42, 230)";

          ctx.font = " bold 72px 微软雅黑";
          ctx.fillText("Hello World! 0", 400, 80);
        })
        .preview({
          top: "10%",
          left: "10%",
        });

      const engine = new Vis.ModelingEngineSupport()
        .setDom(document.getElementById("app"))
        .setSize()
        .setStats(true)
        .registerResources({
          "example.canvas": canvasGenerator.get(),
        });

      const ambientLight = Vis.generateConfig("AmbientLight");

      const canvasTexture = engine.dataSupportManager.reactiveConfig(
        Vis.generateConfig("CanvasTexture", {
          url: "example.canvas",
        })
      );

      const spriteMaterial = Vis.generateConfig("SpriteMaterial", {
        map: canvasTexture.vid,
      });

      const sprite = Vis.generateConfig("Sprite", {
        material: spriteMaterial.vid,
        position: {
          y: 15,
        },
        scale: {
          x: 25,
          y: 5,
        },
      });

      const scene = Vis.generateConfig("Scene", {
        children: [ambientLight.vid, sprite.vid],
      });

      engine.applyConfig(ambientLight, spriteMaterial, sprite, scene);

      engine.setScene(scene.vid).play();

      let number = 0;

      setInterval(() => {
        canvasGenerator.clear().draw((ctx) => {
          ctx.textBaseline = "middle";
          ctx.textAlign = "center";

          ctx.fillStyle = "rgb(206, 42, 230)";

          ctx.font = " bold 72px 微软雅黑";
          ctx.fillText(`Hello World! ${number}`, 400, 80);
        });
        number += 1;
        canvasTexture.needsUpdate = true;
      }, 1000);
    </script>
  </body>
</html>
